$(document).ready(function(){
	 var VAL ={
        NORMAL       : "0000000"  ,
        EVT_USER     : $("#userId").text(),
        T_FIPINQBOX  : 'FIPINQBOX'
    };
	 var domObj={
			 $prdListDiv : $("#prdListDiv"),
			 $prdInfoGrd : $("#prdInfoGrd"),
			 prdInfoPg   :	 "#prdInfoPg",

             $boxListDiv : $("#boxListDiv"),
             $boxInfoGrd : $("#boxInfoGrd"),
              boxInfoPg  :	 "#boxInfoPg",

			 $boxIdTxt	 : $("#boxIdTxt"),
			 buttons:{
				 $queryBox_btn: $("#queryBox_btn"),
				 $prdHisLog_btn:$("#prdHisLog_btn")
			 }
				 
	 };
	 var iniGridInfo=function(){

        var InfoCM = [
            {name: 'prd_seq_id',   		index: 'prd_seq_id',   		label: SHEET_ID_TAG, 		width: '200'},
            {name: 'slot_no',    		index: 'slot_no',   		label: SLOT_NO_TAG, 		width: '60'},
            {name: 'box_id_fk', 		index: 'box_id_fk',			label: BOX_ID_TAG, 	width: '160'},
            {name: 'prd_stat',    		index: 'prd_stat',   		label: PRD_STAT_TAG, 	    width: '160'},
            {name: 'prd_qty',   		index: 'prd_qty',   		label: PRD_QTY_TAG, 		width: '200'},
            {name: 'prd_grade',    		index: 'prd_grade',   		label: PRD_GRADE_TAG, 		width: '200'},
            {name: 'mdl_id_fk',    		index: 'mdl_id_fk',   		label: MDL_ID_TAG, 		width: '200'},
            {name: 'wo_id_fk',    		index: 'wo_id_fk',   		label: WO_ID_TAG, 			width: '200'}
        ];
        domObj.$prdInfoGrd.jqGrid({
            datatype:"local",
            mtype:"POST",
            shrinkToFit:true,
            autowidth:true,
            scroll : false,
            rownumbers : true,
            resizable : true,
//            rowNum:40,
            loadonce:true,
            fixed:true,
            viewrecords:true,
            pager : domObj.prdInfoPg,
            colModel: InfoCM,
            onSelectRow: function(id){}
        });





         var boxInfoCM = [
             {name: 'n_box_id_fk', 		index: 'n_box_id_fk',			label: N_BOX_ID_TAG, 	width: '160'},
             {name: 'box_stat',    		index: 'box_stat',   		label: BOX_STAT_TAG, 	    width: '160'},
             {name: 'prd_qty',   		    index: 'prd_qty',   		label: BOX_QTY_TAG, 		width: '200'},
             {name: 'prd_grade_fk',       index: 'prd_grade_fk',   		label: PRD_GRADE_TAG, 		width: '200'},
             {name: 'mdl_id_fk',    		index: 'mdl_id_fk',   		label: MDL_ID_TAG, 		width: '200'},
             {name: 'wo_id_fk',    		index: 'wo_id_fk',   		label: WO_ID_TAG, 			width: '200'}
         ];
         domObj.$boxInfoGrd.jqGrid({
             datatype:"local",
             mtype:"POST",
             shrinkToFit:true,
             autowidth:true,
             scroll : false,
             rownumbers : true,
             resizable : true,
//            rowNum:40,
             loadonce:true,
             fixed:true,
             viewrecords:true,
             pager : domObj.boxInfoPg,
             colModel: boxInfoCM,
             onSelectRow: function(id){}
         });
	};
	
	var btnFunc={
			query_func: function(){
				var box_id = $.trim(domObj.$boxIdTxt.val());
				if (!box_id) {
					showErrorDialog("","请输入箱号！");
					return;
				}
				var inObj ={
						trx_id : VAL.T_FIPINQBOX,
					    action_flg: 'I',
					  	box_id  : box_id
				};
				var outObj = comTrxSubSendPostJson(inObj);
				if (outObj.rtn_code === VAL.NORMAL) {
					$("#boxIdSpan").html(outObj.box_id);
					$("#boxStatSpan").html(outObj.box_stat);
					$("#entEmptySpan").html(outObj.real_emp);
					$("#prdCntSpan").html(outObj.prd_qty);
					$("#mdlCntSpan").html(outObj.chip_qty);
					$("#mdlIdSpan").html(outObj.mdl_id_fk);
					$("#groupIdSpan").html(outObj.group_id);
					$("#prdGradeSpan").html(outObj.prd_grade_fk);
					$("#prioritySpan").html(outObj.prty);
					$("#destinShopSpan").html(outObj.dest_shop);
					$("#boxUseCntSpan").html(outObj.box_use_cnt);
					$("#maxUseCntSpan").html(outObj.max_use_cnt);
					$("#overRunFlgSpan").html(outObj.box_clean_flg);
					$("#setCodeSpan").html(outObj.box_set_code);
					$("#clearFlgSpan").html(outObj.box_clean_flg);
					if(typeof (outObj.box_clean_flg) !== "undefined"){
                        if(outObj.box_clean_flg.trim()=="Y"){
                            $("#clearFlgSpan").css("color","red");
                        }
					}

					$("#lastClearDateSpan").html("");
					
					$("#pathIdSpan").html(outObj.nx_path_id_fk);
					$("#pathVerSpan").html(outObj.nx_path_ver_fk);
					$("#judgeNoSpan").html(outObj.nx_ope_no_fk);
					$("#judgeIdSpan").html(outObj.nx_ope_id_fk);
					$("#judgeVerSpan").html(outObj.nx_ope_ver_fk);
					$("#procIdSpan").html(outObj.nx_proc_id_fk);
					$("#toolIdSpan").html(outObj.tool_id_fk);
					$("#toolPortIdSpan").html(outObj.tool_port_id_fk);
					
					setGridInfo(outObj.oaryC,"#prdInfoGrd",true);

                    setGridInfo(outObj.oaryD,"#boxInfoGrd",true);


					if(outObj.orayB != null){
                        var oaryb = outObj.oaryB[0];
                        if (oaryb) {
                            $("#positionLoc").html(oaryb.xfr_pos_id);
                            $("#portIdLoc").html(oaryb.xfr_port_id);
                            $("#partIdLoc").html(oaryb.xfr_pati_id);
                            $("#reqionIdLoc").html(oaryb.xfr_zone_id);
                            $("#whShelfIdLoc").html("?");
                            $("#shelfIdLoc").html(oaryb.xfr_shelf_id);
                            $("#poiChangeDateLoc").html(oaryb.evt_date);
                            $("#poiChangeTimeLoc").html(oaryb.evt_timestamp);
                            $("#transStatLoc").html(oaryb.xfr_stat);
                            $("#transStatFlgLoc").html(oaryb.xfr_cmd_flg);
                            $("#fromToolLoc").html(oaryb.xfr_fr_tool_id);
                            $("#fromPortIdLoc").html(oaryb.xfr_fr_port_id);
                            $("#fromPartIdLoc").html(oaryb.xfr_fr_pati_id);
                            $("#fromReqionIdLoc").html(oaryb.xfr_fr_zone_id);
                            $("#toToolLoc").html(oaryb.xfr_to_tool_id);
                            $("#toPortIdLoc").html(oaryb.xfr_to_port_id);
                            $("#toPartIdLoc").html(oaryb.xfr_to_pati_id);
                            $("#toreqionIdLoc").html(oaryb.xfr_to_zone_id);
                            $("#lastShipDateLoc").html(oaryb.xfr_lst_stk_in_date);
                            $("#lastShipTimeLoc").html(oaryb.xfr_lst_stk_in_timestamp);
                            $("#lastPoiBackLoc").html("");
                            $("#lastTrnsReqLoc").html(oaryb.xfr_rtn_code);
                            $("#transDeclDateLoc").html("");
                            $("#transReqTimeLoc").html("");
                            $("#entityEmptyLoc").html("");
                            $("#prdGroupIdLoc").html("?");
                        }
					}
				}
			},
	
			hisLog_func: function(){
				var rowId = domObj.$prdInfoGrd.jqGrid('getGridParam','selrow');
				if (!rowId) {
					showErrorDialog('','请在屏体信息列表中选择一条屏体信息！');
					return;
				}
				var rowData = domObj.$prdInfoGrd.jqGrid('getRowData',rowId);
				var prd_seq_id = rowData.prd_seq_id;
				window.open("./page/9000/9200.jsp?prd_seq_id='"+prd_seq_id+"'");
			}
			
	};
	
	var iniButtonAction=function(){
		domObj.buttons.$queryBox_btn.click(function(){
			btnFunc.query_func();
		});
		domObj.buttons.$prdHisLog_btn.click(function(){
			btnFunc.hisLog_func();
		});
		domObj.$boxIdTxt.keydown(function(event){
			if (event.keyCode == 13) {
				btnFunc.query_func();
			}
		});
	};
	
	 var otherActionBind = function(){
        //Stop from auto commit
        $("form").submit(function(){
            return false;
        });
       
    };
	var clearInfo= function(){
		$("input").val("");
		$("span").html("");
		domObj.$prdInfoGrd.jqGrid("clearGridData");
	}
	
	var initializationFunc = function(){
			clearInfo();
	        iniGridInfo();
	        iniButtonAction();
	        otherActionBind();
	        domObj.$prdInfoGrd.closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "scroll" });  
	        
	    };
	    initializationFunc();
    //enter
   /* document.onkeydown=function(event){
        var e = event || window.event || arguments.callee.caller.arguments[0];
        if(e && e.keyCode==13){ // 按 enter
            $('.bootbox').modal("hide");
        }
    }*/
    //表格自适应
    function resizeFnc() {
        var offsetBottom, divWidth;
        divWidth = $("#divTotal").width();
        offsetBottom =  $(window).height() - $("#divTotal").offset().top;
        $("#divTotal").height(offsetBottom * 0.95);
        $("#prdInfoGrd").setGridWidth(divWidth * 0.99);
        $("#prdInfoGrd").setGridHeight(offsetBottom * 0.9 - 101);
        $("#boxInfoGrd").setGridWidth(divWidth * 0.99);
        $("#boxInfoGrd").setGridHeight(offsetBottom * 0.9 - 101);
    };

    resizeFnc();
    $(window).resize(function () {
        resizeFnc();
    });
});